#include "StdAfx.h"
#include "Deck.h"
using namespace SHGame;
Deck::Deck(void)
{
	this->cards = gcnew LinkedList<Card^>();
	for (int s = 0; s < 4; s++) {
		for (int f = 0; f < 8; f++) {
			Card::Suit suit = Card::SUITS[s];
			Card::Face face = Card::FACES[f];
			Card^ c = gcnew Card(suit, face);
			this->cards->AddFirst(c);
		}
	}
	this->shuffle();
}

Card^ Deck::randomCard() {
	Card::Suit suit = Card::SUITS[Deck::random->Next(4)];
	Card::Face face = Card::FACES[Deck::random->Next(8)];
	Card^ c = gcnew Card(suit, face);
	return c;
}

void Deck::shuffle() {
	array<Card^> ^arr = gcnew array<Card^>(cards->Count);
	IEnumerator<Card^> ^iter = cards->GetEnumerator();
	int i = 0;
	while (iter->MoveNext()) {
		arr[i++] = iter->Current;
	}
	for (int i = 0; i < arr->Length; i++) {
		int randi = Deck::random->Next(arr->Length);
		Card^ temp = arr[randi];
		arr[randi] = arr[i];
		arr[i] = temp;
	}
	cards->Clear();
	for (int i = 0; i < arr->Length; i++) {
		cards->AddFirst(arr[i]);
	}
	return;
}

Card^ Deck::draw() {
	if (! this->hasCards() )
		return nullptr;
	Card^ c = this->cards->First->Value;
	this->cards->RemoveFirst();
	return c;
}

bool Deck::hasCards() {
	return this->cards->Count != 0;
}
